package cn.jimmie.learn.pbc.resource;

import cn.jimmie.learn.sdk.OnSdkLoginListener;

/**
 * function :
 *
 * <p> Create Time:  2019年02月18日 09:41 </p>
 * <p> @author Jimmie.Qian  </p>
 */
public class ShareInstance {
    /*
     * ============== open interface manager ==============
     * OnSdkLoginListener : 对外的登录回调接口
     */
    private OnSdkLoginListener mSdkLoginListener;

    public OnSdkLoginListener getSdkLoginListener() {
        return mSdkLoginListener;
    }

    public void setSdkLoginListener(OnSdkLoginListener sdkLoginListener) {
        this.mSdkLoginListener = sdkLoginListener;
    }

    /*
     * ============== plugin resources ===============
     * ClassLoader : load plugin's class
     */
    private Plugin mPlugin;
    public Plugin getPlugin() {
        return mPlugin;
    }

    public void setPlugin(Plugin plugin) {
        this.mPlugin = plugin;
    }

    /*
     * ================= instance creator =================
     */
    // 声明为 volatile 是为了避免在多线程中,new对象时,指令重排,
    // 造成对象未创建,而判断为非空的情况
    private volatile static ShareInstance sInstance;

    private ShareInstance() {
        // 防止反射调用,被创建出多个实例
        if (sInstance != null)
            throw new RuntimeException("instance exist");
    }

    public synchronized static ShareInstance instance() {
        // 不加锁,判断是否为空, 在锁竞争的情况下,提高性能
        if (sInstance == null) {
            // 只有当为空的时候,加锁创建
            synchronized (ShareInstance.class) {
                if (sInstance == null)
                    sInstance = new ShareInstance();
            }
        }
        return sInstance;
    }
}
